package com.lovo.project.servlet.order;

import com.lovo.project.enums.OrderCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.OrderDO;
import com.lovo.project.service.OrderService;
import com.lovo.project.service.impl.OrderServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "FindByIdServlet",value = "/order/findOrderById")
public class FindByIdServlet extends HttpServlet {
    private OrderService service = new OrderServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {

            Integer orderId = Integer.parseInt(request.getParameter("orderId"));
            OrderDO orderDO = service.findById(orderId);

            System.out.println(orderDO);
            response.getWriter().print(ResultVO.success().add(orderDO).toJson());
        }catch (OurException oe){
            response.getWriter().print(ResultVO.fail(oe.getCodeEnum()).toJson());
        }catch (Exception e){
            response.getWriter().print(ResultVO.fail(OrderCodeEnum.ORDER_UNKNOW_ENUM));
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }
}
